cmake_minimum_required(VERSION 3.12)

set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS_DEBUG "-g3")
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_BUILD_TYPE Release)

PROJECT(LUAEx)
MESSAGE("Lua Source dir "${LUAEx_SOURCE_DIR})
MESSAGE( "Lua Lib Dir:${LUAEx_BINARY_DIR}" )
set(CMAKE_CXX_STANDARD 17)
if(MINGW)
	set (CMAKE_CXX_FLAGS "-std=gnu++17 ${CMAKE_CXX_FLAGS}")
else()
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
endif()


if(WIN32)
	set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DWIN32 -D_WINDOWS")
endif()

if(MSVC)
    add_compile_options("/utf-8")
    add_definitions(-DNOMINMAX)
endif()

if(WIN32 OR CYGWIN)
    add_definitions(-D_USE_MATH_DEFINES)
	add_definitions(-DWIN32)
    list(APPEND EXTERNAL_LIBRARIES winmm wsock32 ws2_32 psapi)
    list(APPEND EXTERNAL_LIBRARIES "windowscodecs")
    list(APPEND EXTERNAL_LIBRARIES "ole32")
    if(NOT MSVC)
		add_compile_options(-liconv)
		list(APPEND EXTERNAL_LIBRARIES iconv)
	endif()
endif()

if(MINGW)
  message(STATUS "The compiler is MINGW.")
  # workaround bugs in mingw with vtable export
  set (CMAKE_SHARED_LINKER_FLAGS "  -Wa,-mbig-obj,-Wl,--export-all-symbols")
  set (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -s")
  set (CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -s")
  #set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -Wa,-mbig-obj" )
endif()

# 检测编译器和操作系统类型
if (MSVC)
    # MSVC 编译器使用 /bigobj
    add_compile_options(/bigobj)
elseif (WIN32 AND (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang"))
    # 对于 Windows 上的 GCC 或 Clang，添加 -Wa,-mbig-obj 选项
    add_compile_options(-Wa,-mbig-obj)
endif()

set(MYLIB_DIR $ENV{MYLIB_DIR})
set(MYLIB_LIB_DIR $ENV{MYLIB_LIB_DIR})
set(MYLIB_BIN_DIR $ENV{MYLIB_BIN_DIR})
set(CMAKE_MODULE_PATH ${MYLIB_DIR}CMAKE_MODULES/)
INCLUDE( ${MYLIB_DIR}CMAKE_MODULES/CollectLibrary.cmake)

# Output directory of libraries (.so, .dylib, non-static .lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${MYLIB_LIB_DIR})
# Output directory of static libraries (.a, static .lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${MYLIB_LIB_DIR})

# For output directory of DLLs (.dll files)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${MYLIB_BIN_DIR})

set(LUA_MATH_EX_STATIC_LIB 0)


find_package(MPI)
message( STATUS "External libraries: ${EXTERNAL_LIBRARIES}")

find_package( Boost REQUIRED)
find_package( Boost REQUIRED COMPONENTS
		thread system filesystem regex locale
		#PATHS  $ENV{BOOST_LIBRARYDIR}/cmake
		)

#set(Boost_USE_STATIC_LIBS        ON)  # only find static libs
find_package( Boost REQUIRED COMPONENTS
		mpi serialization
#		PATHS  $ENV{BOOST_LIBRARYDIR}/cmake
)
#set(Boost_USE_STATIC_LIBS        OFF)  # only find static libs	
	
find_package( Lua REQUIRED )
find_package( GMP )
find_package( OpenGL REQUIRED )
find_package( ftgl )
find_package( Freetype )
find_package( SentinelLDK REQUIRED )
find_package( MyLibLog REQUIRED )
find_package( MyLibGLFont REQUIRED )
find_package( MyLibParsers REQUIRED )
find_package( Sol2 REQUIRED )
find_package( Eigen3 REQUIRED )
find_library( ARPACK_LIB NAMES arpack )

# Find Qt5 itself
find_package(Qt5 COMPONENTS Core Xml OpenGL Gui Svg)
if(NOT Qt5_FOUND)
    message(STATUS "Qt5 not found")
endif()

find_package(OpenMP)
  if(OpenMP_FOUND)
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
  elseif(APPLE AND EXISTS "/opt/local/lib/libomp")
    # just for testing the official Apple compiler with macports' libomp
    set(CMAKE_C_FLAGS
        "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp -I/opt/local/include/libomp")
    set(CMAKE_CXX_FLAGS
        "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp -I/opt/local/include/libomp")
    list(APPEND EXTERNAL_LIBRARIES "-L/opt/local/lib/libomp -lomp")
  elseif(APPLE AND EXISTS "/usr/local/lib/libomp.dylib")
    set(CMAKE_C_FLAGS
        "${CMAKE_C_FLAGS} -Xpreprocessor -fopenmp")
    set(CMAKE_CXX_FLAGS
        "${CMAKE_CXX_FLAGS} -Xpreprocessor -fopenmp")
    list(APPEND EXTERNAL_LIBRARIES "-lomp")
endif()

ADD_SUBDIRECTORY(luafilesystem)
ADD_SUBDIRECTORY(lsqlite3)


ADD_SUBDIRECTORY(luaMPI)
ADD_SUBDIRECTORY(luaQt)
ADD_SUBDIRECTORY(luaState)
ADD_SUBDIRECTORY(luaParameters)
ADD_SUBDIRECTORY(luaNumeric)
#ADD_SUBDIRECTORY(luaRCWABeam2DHelp)
#ADD_SUBDIRECTORY(luaRCWABeamHelp)
#ADD_SUBDIRECTORY(luaRCWAHelp)
ADD_SUBDIRECTORY(luaParametersSweep)
ADD_SUBDIRECTORY(luaPSO)
ADD_SUBDIRECTORY(luaUnity)
ADD_SUBDIRECTORY(luaFont)


